package cn.aukw.todolist;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.aukw.todolist.utils.DBUtils;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_time, tv_main_title, tv_back;
    private EditText et_title, et_content;
    private ImageView iv_del, iv_save;
    private DBUtils dbUtils;
    private int id;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        initView();
        initData();
    }

    private void initView() {
        tv_back = findViewById(R.id.tv_back);
        tv_back.setVisibility(View.VISIBLE);
        et_title = findViewById(R.id.et_title);
        et_content = findViewById(R.id.et_content);
        tv_time = findViewById(R.id.tv_time);
        iv_del = findViewById(R.id.iv_del);
        iv_save = findViewById(R.id.iv_save);
        tv_main_title = findViewById(R.id.tv_main_title);
        tv_back.setOnClickListener(this);
        iv_del.setOnClickListener(this);
        iv_save.setOnClickListener(this);
    }

    private void initData() {
        dbUtils = new DBUtils(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        if (id != 0) {
            tv_main_title.setText("修改记录");
            et_title.setText(intent.getStringExtra("title"));
            et_content.setText(intent.getStringExtra("content"));
            tv_time.setText(intent.getStringExtra("time"));
            tv_time.setVisibility(View.VISIBLE);
        } else {
            tv_main_title.setText("添加记录");
            tv_time.setVisibility(View.GONE);
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:   //返回键的点击事件
                finish();
                break;
            case R.id.iv_del:    //“清空”按钮的点击事件
                et_content.setText("");
                break;
            case R.id.iv_save:   //“保存”按钮的点击事件
                //获取输入的事件标题
                String title = et_title.getText().toString().trim();
                //获取输入的事件内容
                String content = et_content.getText().toString().trim();
                if (id != 0) {//修改界面
                    if (!content.isEmpty()) {
                        if (dbUtils.updateList(id, title, content, DBUtils.getTime())) {
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        } else {
                            showToast("修改失败");
                        }
                    } else {
                        showToast("修改内容不能为空!");
                    }
                } else { //添加记录界面
                    if (!content.isEmpty()) {
                        if (dbUtils.saveList(title, content, DBUtils.getTime())) {
                            showToast("保存成功");
                            setResult(2);
                            finish();
                        } else {
                            showToast("保存失败");
                        }
                    } else {
                        showToast("添加的内容不能为空!");
                    }
                }
                break;
        }
    }


    public void showToast(String message) {
        Toast.makeText(RecordActivity.this, message, Toast.LENGTH_SHORT).show();
    }

}
